/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain.controller.file;

import domain.controller.*;
import domain.model.ICategoria;
import domain.model.file.Categoria;
import java.util.Stack;


/**
 *
 * @author Novo usuário
 */
public class CategoriaController implements ICategoriaController{
    
    private static CategoriaController instance = new CategoriaController();

    private CategoriaController() {
    }

    public static CategoriaController getInstance() {
        return instance;
    }
    public Stack listCategoriaRaiz() throws Exception {

        Stack catsIds = ControllerFileHelper.getIntance().getDescFileNames("catRaizDesc.cfg");
        Stack cats = new Stack();
        for (int i = 0; i < catsIds.size(); i++) {
            String fname = (String)catsIds.elementAt(i);
            cats.addElement(getCategoria(fname));
        }
        return cats;
    }

    public Stack listSubCategoria(ICategoria catPai) throws Exception {

        Stack catsIds = catPai.getCategoriasFilhasIds();
        Stack cats = new Stack();
        for (int i = 0; i < catsIds.size(); i++) {
            String catId = (String)catsIds.elementAt(i);
            cats.addElement(getCategoria(catId));
        }
        return cats;
    }

    public ICategoria getCategoria(int categoriaId) throws Exception {
        return getCategoria(String.valueOf(categoriaId));
    }

    public ICategoria getCategoria(String categoriaId) throws Exception {
        if(categoriaId.indexOf(".") == -1){
            categoriaId += ".cat";
        }
        String stream = ControllerFileHelper.getIntance().readFile(categoriaId);
        return new Categoria(stream);
    }
}
